package com.jxj.controller;

import com.jxj.dao.ContactMsgMapper;
import com.jxj.dao.DepMapper;
import com.jxj.dao.OrgzMapper;
import com.jxj.dao.RecruitMsgMapper;
import com.jxj.entity.*;
import com.jxj.entity.JoinNumVO;
import com.jxj.util.GetSocMapping;
import com.jxj.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class OrgMsgController {
    @Resource
    private OrgzMapper orgzMapper;
    @Resource
    private RecruitMsgMapper recruitMsgMapper;
    @Resource
    private DepMapper depMapper;
    @Resource
    private ContactMsgMapper contactMsgMapper;

    @GetMapping("/organization/allorgmsg")
    public Resp<List<AllOrgVO>> selectAllMsg() {
        List<AllOrgVO> allOrgVOS = orgzMapper.selectAllOrgMsg();
        for (AllOrgVO allOrgVO : allOrgVOS) {
            Integer oId = allOrgVO.getId();
            System.out.println(oId);
            Integer rId = recruitMsgMapper.selectRIdByOId(oId);
            /**
             * @TODO 组织无招新信息处理，来不及的改话可以不处理
             */
            try {
                allOrgVO.setRId(rId);
            } catch (Exception e) {
                log.info("empty msg");
            }
        }
        return new Resp<>(HttpStatus.OK, "查询成功", allOrgVOS);
    }

    @GetMapping("/organization/getorgmsg")
    public Resp<MsgByOIdVO> selectAllMsgByOId(int oId) {
        MsgByOIdVO msgByOIdVO = orgzMapper.selectOrgMsgByOId(oId);
        log.info(msgByOIdVO.toString());
        List<DepartmentMsg> depList = depMapper.selectDepartMsg(oId);
        log.info(depList.toString());
        List<Contact> contactList = contactMsgMapper.selectContact(oId);
        log.info(contactList.toString());
        Map<String, String> depMap = new HashMap<>();
        for (DepartmentMsg departmentMsg : depList) {
            depMap.put(departmentMsg.getDepartment(), departmentMsg.getIntroduce());
        }
        msgByOIdVO.setDepIntroduce(depMap);
        Map<String, String> contactMap = new HashMap<>();
        for (Contact contact : contactList) {
            contactMap.put(contact.getType(), contact.getMsg());
        }
        msgByOIdVO.setContact(contactMap);

        return new Resp<MsgByOIdVO>(HttpStatus.OK, "查询成功", msgByOIdVO);
    }

    @GetMapping("/joinstus")
    public Resp<List<JoinNumVO>> selectEnrollNum(@RequestHeader("Authorization") String token)
    {
        Map<Integer, String> map = GetSocMapping.getMap();
        return new Resp<>(HttpStatus.OK, "查询成功", recruitMsgMapper.selectJoinNum(map.get(JWTUtil.getOrgId(token.split(" ")[1]))));
    }

}
